package org.ossean.gather.sourceDao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.ossean.gather.model.GatherProject;
import org.ossean.gather.model.JobRequirement;
import org.ossean.gather.model.RelativeMemo;

public interface GatherDao {

	// 读取表中最大id值
	@Select("select max(id) from ${table}")
	public int getMaxId(@Param("table") String table);

	// 向gather_tasks表插入转移任务
	@Insert("INSERT ignore INTO ${taskTableName} (BeginId,EndId,SourceTableName,TargetTableName) VALUES (${idsBegin},${idsEnd},#{sourceTableName},#{targetTableName})")
	public int insertLog(@Param("idsBegin") int idsBegin,
			@Param("idsEnd") int idsEnd,
			@Param("taskTableName") String taskTableName,
			@Param("sourceTableName") String sourceTableName,
			@Param("targetTableName") String targetTableName);
	
	/**
	 * 更新pk_control_posts
	 * @param targettable
	 * @param id
	 * @param url_md5
	 */
	@Update("update ${targettable} set id = #{id} where url_md5 = #{url_md5}")
	   public void updatePkControlTable(@Param("targettable")String targettable,@Param("id")int id,
			                            @Param("url_md5")String url_md5);
	// 查询未处理的task
	@Select("SELECT BeginId,EndId,BeginTime,EndTime "
			+ "FROM ${migrationTask} "
			+ "WHERE IsDone = 0 AND SourceTableName=#{sourceTableName} AND TargetTableName=#{targetTableName} ORDER BY BeginId LIMIT 0,1")
	public Map checkDone(@Param("migrationTask") String migrationTask,
			@Param("sourceTableName") String sourceTableName,
			@Param("targetTableName") String targetTableName);

	// 更新转移任务开始时间
	@Update("UPDATE ${migrationTask} SET BeginTime = NOW() WHERE BeginId = ${sourceIdBegin} AND SourceTableName=#{sourceTableName} AND TargetTableName=#{targetTableName}")
	public int updateTaskBeginTime(
			@Param("migrationTask") String migrationTask,
			@Param("sourceIdBegin") int sourceIdBegin,
			@Param("sourceTableName") String sourceTableName,
			@Param("targetTableName") String targetTableName);

	// 获取帖子分表中相应数据 两边都有等于号
	@Select("Select ${selectItems} from ${sourceTable} "
			+ "where id >=${idBegin} and id <= ${idEnd} ${andWhere}")
	public List<RelativeMemo> getPostGatherData(
			@Param("sourceTable") String sourceTableName,
			@Param("selectItems") String selectItems,
			@Param("idBegin") int idBegin, @Param("idEnd") int idEnd,
			@Param("andWhere") String andWhere);

	// 获取项目分表中相应数据 两边都有等于号
	@Select("Select ${selectItems} from ${sourceTable} "
			+ "where id >=${idBegin} and id <= ${idEnd} ${andWhere}")
	public List<GatherProject> getPrjGatherData(
			@Param("sourceTable") String sourceTableName,
			@Param("selectItems") String selectItems,
			@Param("idBegin") int idBegin, @Param("idEnd") int idEnd,
			@Param("andWhere") String andWhere);
	
	// 获取项目分表中相应数据 两边都有等于号
	@Select("Select ${selectItems} from ${sourceTable} "
			+ "where id >=${idBegin} and id <= ${idEnd} and fork=0 ${andWhere}")
	public List<GatherProject> getPrjGatherDataForGit(
			@Param("sourceTable") String sourceTableName,
			@Param("selectItems") String selectItems,
			@Param("idBegin") int idBegin, @Param("idEnd") int idEnd,
			@Param("andWhere") String andWhere);

	// 获取job_requirement中相应数据 两边都有等于号
	@Select("Select ${selectItems} from ${sourceTable} "
			+ "where id >=${idBegin} and id <= ${idEnd} ${andWhere}")
	public List<JobRequirement> getJobGatherData(
			@Param("sourceTable") String sourceTableName,
			@Param("selectItems") String selectItems,
			@Param("idBegin") int idBegin, @Param("idEnd") int idEnd,
			@Param("andWhere") String andWhere);

	// 更新任务结束时间
	@Update("UPDATE ${migrationTask} SET IsDone = 1,EndTime = NOW(),flow=${moveNum} WHERE BeginId = ${endIdBegin} AND SourceTableName=#{sourceTableName} AND TargetTableName=#{targetTableName}")
	public int updateTaskEndTime(@Param("migrationTask") String migrationTask,
			@Param("endIdBegin") int endIdBegin, @Param("moveNum") int moveNum,
			@Param("sourceTableName") String sourceTableName,
			@Param("targetTableName") String targetTableName);

	// 批量获得JobRequirement
	@Select("SELECT * from job_requirements WHERE id > #{id} limit #{batchSize}")
	public List<JobRequirement> getJobRequirementList(@Param("id") int id,
			@Param("batchSize") int batchSize);

}